package com.qf.topic;

import com.qf.utils.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author 郗昊天
 * @version 1.0
 * @package com.qf.simple
 * @date 2022/10/14 20:53
 */
public class Consumer1 {
    private static final String QUEUENAME="xhttopic1";
    private static final String EXCHANGENAME="xhtTopicChange";
    public static void main(String[] args) throws Exception{
        Connection connection = ConnectionUtils.getconnection();
        final Channel channel = connection.createChannel();
        //消费者启动的时候生产者还没有启动 所以要声明队列
        channel.queueDeclare(QUEUENAME,false,false,false,null);
        channel.exchangeDeclare(EXCHANGENAME, "topic");
        channel.queueBind(QUEUENAME, EXCHANGENAME, "user.#");
        //channel.basicQos(1);
        //参数1  从哪个队列中收消息
        //2  是否自动应答
        channel.basicConsume(QUEUENAME, true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                String msg = new String(body);
                System.out.println("消费者收到的消息是" + msg);
                //channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });



    }
}
